/**
 * @todo http请求封装
 * @author 江南小魏晨
 */
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios'

const httpRequest = axios.create({
    timeout: 1000 * 30, // 配置默认最大延迟
    method: 'post', // 配置默认请求方式
    headers: { // 配置默认请求头
        'Content-Type': 'application/JSON;charset=utf-8'
    },
    withCredentials: true // 是否跨域
})
// request拦截
httpRequest.interceptors.request.use(
    (config: AxiosRequestConfig ) => {
        console.log(config, 'requestConfig')
        return config
    }
)
// response拦截
httpRequest.interceptors.response.use(
    (response: AxiosResponse<any>) => {
        const res = response.data
        if (!response.status || response.status !== 200) {
            return Promise.reject(res || 'error')
        } else {
            /*
              请求参数只保留data中内容
             */
            return Promise.resolve(res)
        }
    },
    error => {
        return Promise.reject(error)
    }
)

export default httpRequest
